package tests;

import static org.junit.Assert.*;

import java.sql.Timestamp;

import org.junit.Assert;
import org.junit.Test;

import salesReceiptTransaction.SalesReceipt;
import salesReceiptTransaction.SalesReceiptTransaction;
import schedule.BiWeeklySchedule;
import schedule.MonthlySchedule;
import schedule.PaymentSchedule;
import schedule.WeeklySchedule;
import addEmployeeTransaction.AddCommissionedEmployee;
import addEmployeeTransaction.AddHourlyEmployee;
import addEmployeeTransaction.AddSalariedEmployee;
import addEmployeeTransaction.Employee;
import changeEmployeeTransaction.changeClassificationTransaction.ChangeCommissionedTransaction;
import changeEmployeeTransaction.changeClassificationTransaction.ChangeHourlyTransaction;
import changeEmployeeTransaction.changeClassificationTransaction.ChangeSalariedTransaction;
import changeEmployeeTransaction.changeNameTransaction.ChangeNameTransaction;
import classification.CommissionClassification;
import classification.HourlyClassification;
import classification.PaymentClassification;
import classification.SalariedClassification;
import database.InMemoryPayrollDatabase;
import database.PayrollDatabase;

public class TestTransactions {

	PayrollDatabase database = InMemoryPayrollDatabase.getInstance();
	
	@Test
	@SuppressWarnings("deprecation")
	public void TestSalesReceiptTransaction()
	{
		int empId = 5;
		AddCommissionedEmployee t =
			new AddCommissionedEmployee(
				empId, "Bill", "Home", 2000, 15.25, database);
		t.execute();
		SalesReceiptTransaction tct =
			new SalesReceiptTransaction(
				new Timestamp(2005, 7, 31, 0, 0, 0, 0), 250.00, empId, database);
		try {
			tct.execute();
		} catch (Exception e1) {
			fail("He shouldn't come here, Durrrr!!");
		}

		Employee e = database.getEmployee(empId);
		assertNotNull(e);

		PaymentClassification pc = e.getClassification();
		assertTrue(pc instanceof CommissionClassification);
		CommissionClassification cc = (CommissionClassification) pc;

		SalesReceipt sr = cc.getSalesReceipt(new Timestamp(2005, 7, 31, 0, 0, 0, 0));
		assertNotNull(sr);
		assertEquals(250.00, sr.getSaleAmount(), .001);
	}
	
	@Test
	public void TestChangeNameTransaction() {
		int empId = 2;
		AddHourlyEmployee t = new AddHourlyEmployee(empId, "Bill", "Home", 15.25, database);
		t.execute();
		ChangeNameTransaction cnt = new ChangeNameTransaction(empId, "Bob", database);
		cnt.execute();
		Employee e = database.getEmployee(empId);
		assertNotNull(e);
		assertEquals("Bob", e.getName());
	}
	
	@Test
	public void TestChangeHourlyTransaction() {
		int empId = 3;
		AddCommissionedEmployee t =
			new AddCommissionedEmployee(
				empId, "Lance", "Home", 2500, 3.2, database);
		t.execute();
		ChangeHourlyTransaction cht =
			new ChangeHourlyTransaction(empId, 27.52, database);
		cht.execute();
		Employee e = database.getEmployee(empId);
		assertNotNull(e);
		PaymentClassification pc = e.getClassification();
		assertNotNull(pc);
		assertTrue(pc instanceof HourlyClassification);
		HourlyClassification hc = (HourlyClassification) pc;
		assertEquals(27.52, hc.getHourlyRate(), .001);
		PaymentSchedule ps = e.getSchedule();
		assertTrue(ps instanceof WeeklySchedule);
	}
	
	@Test
	public void TestChangeSalaryTransaction() {
		int empId = 4;
		AddCommissionedEmployee t =
			new AddCommissionedEmployee(
				empId, "Lance", "Home", 2500, 3.2, database);
		t.execute();
		ChangeSalariedTransaction cst =
			new ChangeSalariedTransaction(empId, 3000.00, database);
		cst.execute();
		Employee e = database.getEmployee(empId);
		assertNotNull(e);
		PaymentClassification pc = e.getClassification();
		assertNotNull(pc);
		assertTrue(pc instanceof SalariedClassification);
		SalariedClassification sc = (SalariedClassification) pc;
		assertEquals(3000.00, sc.getSalary(), .001);
		PaymentSchedule ps = e.getSchedule();
		assertTrue(ps instanceof MonthlySchedule);
	}
	
	@Test
	public void TestChangeCommisionTransaction() {
		int empId = 5;
		AddSalariedEmployee t =
			new AddSalariedEmployee(
				empId, "Bob", "Home", 2500.00, database);
		t.execute();
		ChangeCommissionedTransaction cht =
			new ChangeCommissionedTransaction(empId, 1250.00, 5.6, database);
		cht.execute();
		Employee e = database.getEmployee(empId);
		assertNotNull(e);
		PaymentClassification pc = e.getClassification();
		assertNotNull(pc);
		assertTrue(pc instanceof CommissionClassification);
		CommissionClassification cc = (CommissionClassification) pc;
		assertEquals(1250.00, cc.getBaseRate(), .001);
		assertEquals(5.6, cc.getCommissionRate(), .001);
		PaymentSchedule ps = e.getSchedule();
		assertTrue(ps instanceof BiWeeklySchedule);
	}	
}